package com.jinghua.treatment.common.controller;

import com.jinghua.treatment.common.Pager;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.SysDoctor;
import com.jinghua.treatment.service.SysDoctorService;
import com.jinghua.treatment.tools.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;


/**
 * Created by kriss on 2018/4/26.
 */
@Component
public class BaseController {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected HttpHeaders headers;
    protected Integer page;
    protected Pager pager;


    //临时 测试用
    @Autowired
    private SysDoctorService sysDoctorService;

    @ModelAttribute
    protected void initRequestResponseSessionHeaders(HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     @RequestHeader HttpHeaders headers,
                                                     RedirectAttributes redirectAttributes) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        this.headers = headers;

        String pageStr = request.getParameter("page");

        //去除参数空格和四个字节特殊字符 如 imojo
        Map<String,String[]> map = request.getParameterMap();
        Set<String> keySet = map.keySet();
        for(String key:keySet){
            String datas[] = map.get(key);
            for(int i = 0 ; i < datas.length;i++){
                if(datas[i]!=null){
                    datas[i]=datas[i].replace("/[\\xF0-\\xF7].../s","");
                    datas[i]=datas[i].trim();
                }
            }
        }

        /*SysDoctor  sysDoctor =  sysDoctorService.findById(12);
        session.setAttribute("currentDoctor",sysDoctor);*/

        pager = new Pager();
        if (StringUtils.isNotEmpty(pageStr)) {
            logger.debug("####页码参数page:" + pageStr);
            page = Integer.parseInt(pageStr);
            pager.setPage(page);
        }
    }
    protected JSONResult jsonResult(String code, String msg, Object data) {
        return new JSONResult(code, msg, data);
    }

    protected String uploadImge(MultipartFile file,String appPath,String prefix){
        String storePath = prefix+ "/" +StringUtil.mkFileName(".jpg");
        FileOutputStream fos = null;
        InputStream is = null;
        try {
            is = file.getInputStream();
            fos  =new FileOutputStream(appPath+"/"+storePath);
            byte[] data=new byte[1024];
            int readed  = 0 ;
            while ((readed = is.read(data))>0) {
                fos.write(data,0,readed);
            }
        } catch (IOException e) {
            e.printStackTrace();
            storePath = "";
        }finally {
            try {
                is.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
                storePath = "";
            }
        }
        return storePath ;
    }
}
